﻿using System;

class LargestNumberInArray

            // Write a program, that reads from the console an array of N integers and an integer K, sorts the array and using the method:
            // Array.BinSearch() finds the largest number in the array which is ≤ K. 

{
    static void Main()
    {
        Console.Write("Enter the length of the array: ");
        int arraySize = int.Parse(Console.ReadLine());
        int[] array = new int[arraySize];
        Console.Write("Enter value for K: ");
        int k = int.Parse(Console.ReadLine());
        Console.WriteLine();

        Console.WriteLine("Enter the elements of array: ");
        for (int i = 0; i < arraySize; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine();

        Array.Sort(array, (a, b) => a.CompareTo(b));

        int bestValue = Array.BinarySearch(array, k);

        if (bestValue < -1)
        {
            Console.WriteLine("The largest number in the array that is <= {0} is: {1}", k, array[~bestValue - 1]);
        }
        else if (~bestValue == 0)
        {
            Console.WriteLine("No element in the array is <= {0}!", k);
        }
    }
}

